package leetcode;

import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.List;

public class Solution10 {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        if(root == null){
            return res;
        }
        List<Integer> paths = new ArrayList<>();
        travel(root,paths,res);
        return res;
    }

    private void travel(TreeNode root, List<Integer> paths, List<String> res){
        //中
        paths.add(root.val);
        //判断终止条件
        if(root.left == null && root.right == null){
            StringBuilder sb = new StringBuilder();
            for(int i = 0 ; i < paths.size() - 1;i++){
                sb.append(paths.get(i)).append("->");
            }
            sb.append(paths.get(paths.size() - 1));
            res.add(sb.toString());
            return ;
        }
        if(root.left != null){
            travel(root.left,paths,res);
            paths.remove(paths.size() - 1);
        }
        if(root.right != null){
            travel(root.right,paths,res);
            paths.remove(paths.size() - 1);
        }

    }
}
